package com.mic.endpoint.api;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

/**
 * Generates an api-file for an appengine-application.
 * @goal generate
 */
public class EndpointApiMojo extends AbstractMojo {

    /**
     * The classpath of the service-classes.
     *
     * @parameter expression="${classPath}" default-value="${basedir}/target/classes"
     */
    private String classPath;

    /**
     * The full qualified names of the service classes.
     *
     *@parameter
     */
    private String serviceClassNames;

    /**
     * The appId of your appengine application.
     *
     * @parameter expression="${applicationId}" default-value=""
     */
    private String applicationId;

    /**
     * The directory for the generated api-file.
     *
     * @parameter expression="${apiOutputDirectory}" default-value="${basedir}/src/main/webapp/WEB-INF"
     */
    private String apiOutputDirectory;


    /**
     * 
     */
    public void execute() throws MojoExecutionException, MojoFailureException {

        try {
            List<String> classNames =Arrays.asList(serviceClassNames.split(","));
            EndpointApiGenerator endpointApiGenerator = new EndpointApiGenerator();

            getLog().info(
                    new StringBuilder("Generating api-file for appId ").append(applicationId).append(" with classes ")
                            .append(serviceClassNames.toString()).toString());
            Map<String, String> apiConfigs = endpointApiGenerator.generateApiConfigs(applicationId, classPath, classNames);

            getLog().info(new StringBuilder("Writing api-file to ").append(apiOutputDirectory).toString());
            endpointApiGenerator.write(apiConfigs, apiOutputDirectory);
        } catch (Exception e) {
            getLog().error(e);
            throw new MojoExecutionException("Error while generating api-file!", e);
        }

    }

}
